<?php
/*

*/

  /* ============================
    Liste des variables attendues
   ------------------------------
     POST ACTION_IDENTIFICATION   = CO|DECO
     POST LOGIN_IDENTIFICATION    = string
     POST PASSWORD_IDENTIFICATION = password
   =========================== */

$action = (!empty($_POST['ACTION_IDENTIFICATION']))   ? cleanVar($_POST['ACTION_IDENTIFICATION'])   : null;
$login  = (!empty($_POST['LOGIN_IDENTIFICATION']))    ? cleanVar($_POST['LOGIN_IDENTIFICATION'])    : null;
$pass   = (!empty($_POST['PASSWORD_IDENTIFICATION'])) ? cleanVar($_POST['PASSWORD_IDENTIFICATION']) : null;

$nbrAction = 0;

try
{

  if(is_null($action))
    {
      throw new ListerException(15);
    }

  // demande de déconnexion
  if($action == 'DECO')
    { 
      unset($_SESSION['IDENTIFICATION_OK']);
      unset($_SESSION['IS_ADMIN']);
      unset($_SESSION['IS_UPLOADER']);

      $nbrAction++;
    }

  // demande de connexion
  if($action == 'CO' && !is_null($pass) && !is_null($login))
    {
      // on test le login (throw si le login n'existe pas)
      $identification = new Utilisateur($login);

      if (!$identification->estActif())
	{
	  throw new ListerException(191);
	}

      if(!$identification->estValide(md5($pass)))
	{
	  throw new ListerException(19);
	}

      $_SESSION['IP']                = $_SERVER['REMOTE_ADDR'];
      $_SESSION['IDENTIFICATION_OK'] = true;
      $_SESSION['LOGIN']             = $login;
      $_SESSION['NOM']               = $identification->getNom();
      $_SESSION['PRENOM']            = $identification->getPrenom();
      $_SESSION['IS_ADMIN']          = $identification->getIsAdmin();
      $_SESSION['IS_UPLOADER']       = $identification->getIsUploader();

      $nbrAction++;
      $template->assign_vars($identification->returnArray());
    }
  
  if($nbrAction == 0) throw new ListerException(15);

  $ok = true;
  $template->assign_vars(array('OK' => 'ok'));
  $template->assign_vars(array('NOMBRE_ACTION' => $nbrAction));
}
catch(ListerException $e)
{
  $template->assign_vars($e->returnArray());
  $template->assign_vars(array('OK'  => 'err'));
}

?>
